﻿using System;
using System.Net;
using System.Net.Sockets;
using System.ComponentModel;
using FinosLibrary;
using System.Threading;
using System.Linq.Expressions;
using System.Windows;
namespace FinosLibrary{
    public class ModelLogic:Logic {
        public ResourceDictionary resource { get { return Application.Current.Resources; } }
        public static NetConnection connection;
        private bool _isEnabled;
        public bool _IsEnabled {
            get { return !_isEnabled; }
        }
        public bool IsEnabled {
            get { return _isEnabled; }
            set {
                _isEnabled = value;
                this.NotifyPropertyChanged(x => x.IsEnabled);
                this.NotifyPropertyChanged(x => x._IsEnabled);
            }
        }

        public virtual void NetConnection_Connected() {
            IsEnabled = true;
        }

        public virtual void NetConnection_Disconnected(string reason) {
            IsEnabled = false;
        }
        public ModelLogic()
            : base() {
                IsEnabled = false;
                if (this is NetConnection) {
                    connection = this as NetConnection;
                }
        }
        
        public override void _NotifyPropertyChanged(string propertyName) {
            syn.Post(base._NotifyPropertyChanged, propertyName);
        }
        public void call(string fun, object b) {
            connection.SendPackage(Inbox(new Package(fun, b)));
        }
        public void call(string fun) {
            connection.SendPackage(Inbox(new Package(fun)));
        }
        public void send(byte[] bytes) {
            connection.SendPackage(Inbox(new Package() { byteBody = bytes }));
        }
        private Package Inbox(Package p) {
            if (app != "" && app != null) {
                return new Package("PropertyPagePackage", new Package(app, p));
            }
            return p;
        }
        public void callLocal(string fun,object b) {
            doLogic(new Package(fun, b),app);
        }
        public void callLocal(string fun) {
            doLogic(new Package(fun),app);
        }
    }
}
